<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<title>商品编辑/新增</title>
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="css/bootstrap.min.css" />
	</head>

	<body>
		<div class="container" style="padding-top: 100px;" >

			<div class="starter-template">
				<h2 style="text-align: center;color: #7266ba;">商品编辑/新增</h2>
				<form>
					<div class="form-group">
						<label for="exampleInputEmail1">商品名称</label>
						<input type="text" class="form-control" id="title" placeholder="商品名称">
					</div>
					<div class="form-group">
						<label for="exampleInputPassword1">价格</label>
						<input type="text" class="form-control" id="price" placeholder="价格">
					</div>
					<div class="form-group">
						<label for="exampleInputPassword1">库存</label>
						<input type="text" class="form-control" id="stock" placeholder="库存">
					</div>
					<div class="form-group">
						<label for="exampleInputPassword1">商品描述</label>
						<input type="text" class="form-control" id="description" placeholder="商品描述">
					</div>
					<div class="form-group">
						<label for="exampleInputPassword1">商品图片</label>
						<input type="text" class="form-control" id="imgUrl" placeholder="商品图片">
					</div>
					<button id="addItem" type="button" class="btn btn-success">添加</button>
					<button id="toItem" type="button" class="btn btn-primary">先去查看商品列表</button>
				</form>
			</div>

		</div>

		<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
		<script type="text/javascript" src="js/bootstrap.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("#toItem").on("click",function(){
					window.location.href="itemList.html";
				});
				
				$("#addItem").on("click",function(){
					var title = $("#title").val();
					var price = $("#price").val();
					var stock = $("#stock").val();
					var description = $("#description").val();
					var imgUrl = $("#imgUrl").val();
					if(title == null || title ==""){
						alert("商品名称不能空");
						return false;
					}
					if(price == null || price ==""){
						alert("商品价格不能空");
						return false;
					}
					if(stock == null || stock ==""){
						alert("库存数不能空");
						return false;
					}
					if(description == null || description ==""){
						alert("商品描述不能空");
						return false;
					}
					if(imgUrl == null || imgUrl ==""){
						alert("商品图片不能空");
						return false;
					}
					$.ajax({
						type:"post",
						url:"http://127.0.0.1:8090/item/create",
						data:{
							"title":title,
							"price":price,
							"stock":stock,
							"description":description,
							"imgUrl":imgUrl
						},
						async:true,
						xhrFields:{withCredentials:true},
						success:function(msg){
						if(msg.status=="success"){
							alert("商品添加成功");
						}else{
							alert("商品添加失败，原因为"+msg.data.Msg);
						}
						},
						error:function(err){
							console.log(err.responseText);
						}
					});
				})
			});
		</script>
	</body>

</html>